$(function () {
//声明变量
    //数据类型表格/数据明细
    var dictionary_datagrid = $("#dictionary_datagrid");
    var select_datagrid =$("#select_datagrid");
    var choseGiftDatagrid = $("#choseGiftDatagrid");
    //数据类型弹出框
    var dictionary_dialog =$("#dictionary_dialog");
    var dictionarydetail_dialog =$("#dictionarydetail_dialog");
    //数据类型弹出表单
    var dictionary_form = $("#dictionary_form");
    var dictionarydetail_form = $("#dictionarydetail_form");
    var open_dialog = $("#open_dialog");
    var gift_datagrid =   $("#gift_datagrid");
    //菜单抽取
    var menu_combotree = $("#menu_combotree");
    //数据类型列表
    dictionary_datagrid.datagrid({
        url:'/member/query.do',
        width:1350,
        height:500,
        fitColumns:true,
        toolbar:'#member_toolbar',
        pagination:true,
        rownumber:true,
        singleSelect:true,
        columns:[[
            {field:'id',checkbox:true},
            {field:'cardNumber',title:'会员卡号',width:80},
            {field:'name',title:'会员名称',width:80},
            {field:'tel',title:'电话',width:80},
            {field:'birthday',title:'会员生日',width:80},
            {field:'levelName',title:'会员等级',width:80},
            {field:'groupName',title:'会员分组',width:80},
            {field:'employeeName',title:'开卡员工',width:80},
            {field:'balance',title:'余额',width:80},
            {field:'address',title:'地址',width:80},
            {field:'points',title:'积分',width:80},
            {field:'sourceName',title:'客户来源',width:80}
        ]],
        onDblClickRow:function (index,row) {
            var row = $("#dictionary_datagrid").datagrid('getSelected')
            console.log(row);
            var id = row.id;
            var cardNumber = row.cardNumber;
            var name = row.name;
            var levelName = row.levelName;
            var birthday = row.birthday;
            var balance = row.balance;
            var points = row.points;
            var number = row.number;
            var sourceName = row.sourceName;
            $("#id").val(id);
            $("#cardNumber").val(cardNumber);
            $("#name").val(name);
            $("#levelName").val(levelName);
            $("#birthday").val(birthday);
            $("#balance").val(balance);
            $("#points").val(points);
            $("#number").val(number);
            $("#sourceName").val(sourceName);
            open_dialog.dialog('close');
            $("#member_datagrid").datagrid('reload')
        },
        onClickRow:function (index,row) {
            //如果选中行有id,则可以查询明细列表
            if (row.id) {
                select_datagrid.datagrid({
                    url:'/member/selectAll.do?id='+row.id,
                    toolbar:'#dictionarydetail_toolbar',
                    fit:true,
                    fitColumns:true,
                    singleSelect:true,
                    columns:[[
                        {field:'id'},
                        {field:'name',title:'会员姓名',width:80,align:'center'},
                        {field:'cardNumber',title:'会员卡号',width:80,align:'center'},
                        {field:'level',title:'会员等级',width:80,align:'center'},
                        {field:'birthday',title:'生日',width:80,align:'center'},
                        {field:'balance',title:'卡上余额',width:80,align:'center'},
                        {field:'points',title:'当前积分',width:80,align:'center'},
                        {field:'source',title:'会员来源',width:80,align:'center'}
                    ]]
                });
            }
        }
    });

    //数据类型列表

    //配置数据类型  dialog,form表单
    dictionary_dialog.dialog({
        width:500,
        height:500,
        buttons:'#form_btn',
        close:true,
        onClose:function () {
            //关闭的同时清除表单数据
            dictionary_form.form('clear');
        }
    });
    open_dialog.dialog({
        title: 'My Dialog2',
        width: 600,
        height: 200,
        closed: true,
        cache: false,
        /*href: '/gift/query.do',*/
        modal: true
    });
    //配置数据类型  dialog,form表单
    dictionarydetail_dialog.dialog({
        width:300,
        height:200,
        buttons:'#detailform_btn',
        close:true,
        onClose:function () {
            //关闭的同时清除表单数据
            dictionarydetail_form.form('clear');
        }
    });

    gift_datagrid.datagrid({
        url:'/gift/query.do',
        height:200,
        width:600,
        fitColumns:true,
        toolbar:'#gift_toolbar',
        rownumber:true,
        singleSelect:true,
        columns: [[
            {field: 'sn', title: '礼品编号', width: 100},
            {field: 'name', title: '礼品名称', width: 100},
            {field: 'unit', title: '单位', width: 100},
            {field: 'inte', title: '所需积分', width: 100},
            {field: 'endNumber', title: '剩余数量', width: 100},
            {
                field: 'number', title: '数量', width: 100, formatter: function (value, row, index) {
                    return '<input type="number" id="takeNum" value="1" style="width:40px;" min="1" onclick="amount(this)">'
                }
            }
        ]]
       /* onDblClickRow:function (index,row) {
            var row = $("#gift_datagrid").datagrid('getSelected')
            var sn = row.sn;
            var name = row.name;
            var until = row.until;
            var inte = row.inte;
            var endNumber = row.endNumber;
            $("#sn").val(sn);
            $("#name").val(name);
            $("#until").val(until);
            $("#inte").val(inte);
            $("#endNumber").val(endNumber);
            open_dialog.dialog('close');
            $("#gift_datagrid").datagrid('reload')
        }*/
    });
    choseGiftDatagrid.datagrid({
        url: '/gift/query.do',
        height:200,
        width:600,
        fitColumns: true,
        pagination: true,
        singleSelect: true,
        rownumbers: true,
        toolbar: '#giftChosen_toolbar',
        scrollbarSize: 0,
        columns: [[
            {field: 'sn', title: '礼品编号', width: 100},
            {field: 'name', title: '礼品名称', width: 100},
            {field: 'unit', title: '单位', width: 100},
            {field: 'inte', title: '所需积分', width: 100},
            {field: 'endNumber', title: '剩余数量', width: 100}
        ]],
        onClickRow: function (index, row) {
            var selRows = gift_datagrid.datagrid("getRows");
            if (!selRows[0]) {
                gift_datagrid.datagrid("appendRow", row);
            } else {
                gift_datagrid.datagrid("deleteRow", 0);
                gift_datagrid.datagrid("appendRow", row);
            }
            $("#open_dialog").dialog("close");
            $("#needIntegral").html(row.inte);
        }
    });
    var methodObj = {
        add:function(){
            dictionary_dialog.dialog('open');
            dictionary_dialog.dialog('setTitle','新增数据类型');
        },
        query:function (){
            //获取查询条件
            var keyword =  $("#keyword").textbox('getValue');
            //通过datagrid 的 load方法传递参数
            dictionary_datagrid.datagrid('load',{
                keyword:keyword
            });
        },
        select:function () {
            open_dialog.dialog('open');
        },
        have: function () {
            //获取礼品选择行
            var row = $("#gift_datagrid").datagrid("getRows")[0];
            //获取保存属性值
            var id = row.id;
            var cardNumber = row.cardNumber;
            var name = row.name;
            var unit = row.unit;
            var takeNum =$("#takeNum").val();
            var points = row.inte;
            $.get("/gift/updatePoint.do",{
                id:id,
                cardNumber:cardNumber,
                name:name,
                unit:unit,
                points:points,
                number:takeNum
            }, function (data) {
                if (data.success) {
                    $.messager.alert('温馨提示', "保存成功", 'info', function () {
                        gift_datagrid.datagrid('reload');
                        location.reload();
                    });
                } else {
                    $.messager.alert('温馨提示',"保存失败", 'error');
                }
            })
        }
    };
    //统一绑定事件:
    $("a[data-cmd]").click(function () {
        var methodname = $(this).data("cmd");
        //动态获取调用方法名字,调用方法;
        methodObj[methodname]();
    });

});